import React from 'react';
import { Switch, Route } from 'react-router-dom';
import { interceptorRoute, IRoutersData } from './index';
import NoMatch from '../components/Layout/404';
import ErrorBoundary from './ErrorBoundary';

// 自动引入childRoutes目录里的子路由
const files = require.context('./childRoutes', false, /\.(js|ts)$/);
const routeList: IRoutersData = [];
files.keys().forEach((key) => {
  const child = files(key).default;
  routeList.push(...child);
});

const SubRoute = () => {
  return (
    <ErrorBoundary>
      <Switch>
        {routeList.map((item) => {
          return interceptorRoute(item);
        })}
        <Route component={NoMatch} key="noMatch" />
      </Switch>
    </ErrorBoundary>
  );
};

export { routeList };
export default SubRoute;
